#######################################################
# CREATE TIMELINE OF INSURGENT MOVEMENTS AND NETWORKS #
#######################################################
# Author: Kasia Nalewajko
# First created: 04 March 2022
# Replicated: 29 May 2024

rm(list = ls())

# LOAD PACKAGES -----------------------------------------------------------

if (!require("dplyr")) install.packages("dplyr")
if (!require("tidyr")) install.packages("tidyr")
if (!require("stringr")) install.packages("stringr")
if (!require("vistime")) install.packages("vistime")
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("forcats")) install.packages("forcats")
if (!require("remotes")) install.packages("remotes")
# remotes::install_github("AllanCameron/geomtextpath")
library(geomtextpath)

# LOAD DATA ---------------------------------------------------------------

guerin2010 <- readxl::read_excel("./00 SUBMITTED/00 APSR final/04 replication_files/01 data/guerin2010.xlsx")

# CLEAN DATA ---------------------------------------------------------------

guerin2010 <- guerin2010 %>% 
  dplyr::select(name, type, start_day, start_month, start_year, end_day, end_month, end_year, nmembers) %>% 
  dplyr::mutate(start_date = as.Date(paste0(guerin2010$start_year, "-", guerin2010$start_month, "-", guerin2010$start_day))) %>% 
  dplyr::mutate(end_date = as.Date(paste0(guerin2010$end_year, "-", guerin2010$end_month, "-", guerin2010$end_day)))

guerin2010 <- guerin2010[!is.na(guerin2010$start_date),]

# PLOT ---------------------------------------------------------------

guerin2010 %>% 
  mutate(name = forcats::fct_reorder(name, as.Date(start_date))) %>%
  ggplot() +
  geom_segment(aes(x = as.Date(start_date), xend = as.Date(end_date), y = name, yend = name, colour = type, size = nmembers), alpha = 0.5) +
  geom_textvline(xintercept = as.Date("1942-03-27"), color = "red", size = 3, vjust = -0.2, hjust = 0.95, label = "Convoy 1") +
  geom_textvline(xintercept = as.Date("1942-06-22"), color = "red", size = 3, vjust = -0.2, hjust = 0.95, label = "Convoy 3") +
  geom_textvline(xintercept = as.Date("1944-08-17"), color = "red", size = 3, vjust = -0.2, hjust = 0.95, label = "Convoy 79") +
  scale_x_continuous(limits = c(as.Date("1940-01-01"), as.Date("1945-06-01")),
                     breaks=c(as.Date("1940-01-01"), as.Date("1941-01-01"), as.Date("1942-01-01"), as.Date("1942-03-27"), as.Date("1942-06-22"), as.Date("1943-01-01"), as.Date("1944-01-01"), as.Date("1944-08-17"), as.Date("1945-01-01")),
                     labels=c("1940", "1941", "1942", "27.03.1942", "22.06.1942", "1943", "1944", "17.08.1944", "1945")) +
  labs(
    title = "",
    x = "Date",
    y = "Insurgent groups",
    colour = "Group type",
    size = "Number members") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        panel.background = element_rect(fill = "transparent",
                                        colour = NA_character_),
        plot.background = element_rect(fill = "transparent",
                                       colour = NA_character_))  +
  scale_colour_viridis_d(option = "inferno",
                         begin = 0.2, end = 0.85)

# SAVE ---------------------------------------------------------------

ggsave(filename = "1_sequence_timeline.png",
       plot = last_plot(),
       path = "./00 SUBMITTED/00 APSR final/01 main_text/figures",
       width = 20,
       height = 10,
       units = "cm",
       dpi = 300)

